package com.cloudptt.wp.ui.address.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.message.SessionActivity;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupInfo;
import com.mcptt.db.model.UserInfo;

import java.util.List;

public class AddrListExpandableAdapter extends BaseExpandableListAdapter {

	private static int DAC_TYPE = 0;
	private static int CAMERA_TYPE = 1;
	private static int USER_TYPE = 2;
	private static int[] Drawable_online = {R.drawable.dac_icon_background_online, R.drawable.dac_icon_background_online, R.drawable.user_icon_background_online};
	private static int[] Drawable_offline = {R.drawable.dac_icon_background_offline, R.drawable.dac_icon_background_online, R.drawable.user_icon_background_offline};

	private static String TAG = "AddrListAdapter";
	private static final boolean DEBUG = true;
	private Context ctx = null;
	private List<GroupInfo> groupList = null;
	private AddrInfo curGroupInfo =null;
	private AddrInfo mcIdInfo = null;

	public AddrListExpandableAdapter(Context ctx, List<GroupInfo> groupList, AddrInfo curGroupInfo, AddrInfo mcIdInfo)
	{
		Log.d(TAG, "AddrListAdapter");
		this.ctx = ctx;
		this.groupList = groupList;
		this.curGroupInfo = curGroupInfo;
		this.mcIdInfo = mcIdInfo;
	}

	public void refresh(AddrInfo curGroupInfo,List<GroupInfo> list){
		Log.d(TAG, "flashData list = " + list + " curGroupInfo = " + curGroupInfo);
		this.curGroupInfo = curGroupInfo;
		this.groupList = list;
		this.notifyDataSetChanged();
	}

	public int getGroupCount() {   
		if(groupList == null
				|| groupList.size() <= 0)
		{
			return 0;
		}
		return groupList.size();   
	}

	public long getGroupId(int groupPosition) {   
		return groupPosition;   
	}

	public View getGroupView(int groupPosition, boolean isExpanded,   
			View convertView, ViewGroup parent) {   

		GroupViewHolder groupViewHolder;
		if (convertView == null){
			LayoutInflater inflater = LayoutInflater.from(ctx);
			convertView = inflater.inflate(R.layout.item_group, parent, false);

			groupViewHolder = new GroupViewHolder();
			groupViewHolder.userInfoLy =  (View)  convertView.findViewById(R.id.user_info);
			groupViewHolder.name    =  (TextView)  convertView.findViewById(R.id.user_name);
			//			groupViewHolder.number   =  (TextView)  convertView.findViewById(R.id.user_number);
			groupViewHolder.userIcon = (ImageView)convertView.findViewById(R.id.user_icon);
			groupViewHolder.defaultTv = (TextView)convertView.findViewById(R.id.defaultTv);
//			groupViewHolder.curTv = (TextView)convertView.findViewById(R.id.curTv);
			groupViewHolder.indicator = (ImageView)convertView.findViewById(R.id.indicator_custom);
			groupViewHolder.divider = convertView.findViewById(R.id.group_divider);

			convertView.setTag(groupViewHolder);
		}else{
			groupViewHolder = (GroupViewHolder) convertView.getTag();
		}

		final GroupInfo groupInfo = groupList.get(groupPosition);

		groupViewHolder.userInfoLy.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				goToMsgActivity(groupInfo);
			}
		});

		String name = groupInfo.getName();
		//		String number = groupInfo.getMcId();

		groupViewHolder.name.setText(name);
		if(groupInfo.isAffiliation()){
			groupViewHolder.defaultTv.setText(R.string.name_default);
			groupViewHolder.defaultTv.setVisibility(View.VISIBLE);
		}else{
			groupViewHolder.defaultTv.setText("");
			groupViewHolder.defaultTv.setVisibility(View.GONE);
		}
		Log.d(TAG, "groupInfo = " + groupInfo + " defaultGroupInfo = " + curGroupInfo);
//		if(groupInfo.isSameOne(curGroupInfo)){
//			groupViewHolder.curTv.setText(R.string.name_cur);
//			groupViewHolder.curTv.setVisibility(View.VISIBLE);
//		}else{
//			groupViewHolder.curTv.setText("");
//			groupViewHolder.curTv.setVisibility(View.GONE);
//		}

		if(isExpanded){
			groupViewHolder.indicator.setImageResource(R.drawable.group_expandable_indicator_down);
		}else{
			groupViewHolder.indicator.setImageResource(R.drawable.group_expandable_indicator_up);
		}

		if(groupPosition == 0){
			groupViewHolder.divider.setVisibility(View.GONE);
		}else{
			groupViewHolder.divider.setVisibility(View.VISIBLE);
		}

		return convertView;
	}

	public UserInfo getChild(int groupPosition, int childPosition) 
	{   

		GroupInfo info = groupList.get(groupPosition);
		List<UserInfo> userInfoList = info.getMemberList();
		if(userInfoList != null
				&& userInfoList.get(childPosition) != null)
		{
			return userInfoList.get(childPosition);
		}
		return null;   
	}   

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;   
	}  

	public int getChildrenCount(int groupPosition) {   
		if(groupList == null
				|| groupList.size() <= 0)
		{
			return 0;
		}
		GroupInfo info = groupList.get(groupPosition);
		if(info == null)
		{
			return 0;
		}
		List<UserInfo> userInfoList = info.getMemberList();
		if(userInfoList != null)
		{
			return userInfoList.size();
		}
		return 0;
	}   

	public View getChildView(int groupPosition, int childPosition,   
			boolean isLastChild, View convertView, ViewGroup parent) 
	{ 

		ChildViewHolder childViewHolder;
		if (convertView == null){
			LayoutInflater inflater = LayoutInflater.from(ctx);
			convertView = inflater.inflate(R.layout.item_user, parent, false);

			childViewHolder = new ChildViewHolder();
			childViewHolder.userInfoLy =  (View)  convertView.findViewById(R.id.user_info);
			childViewHolder.name    =  (TextView)  convertView.findViewById(R.id.user_name);
			childViewHolder.videoBtn  =  (ImageView) convertView.findViewById(R.id.video_button);
			childViewHolder.userIcon = (ImageView)convertView.findViewById(R.id.user_icon);
			childViewHolder.voiceBtn = (ImageView) convertView.findViewById(R.id.call_button);
			childViewHolder.mySelf = (TextView)convertView.findViewById(R.id.is_myself);
			childViewHolder.divider = convertView.findViewById(R.id.child_divider);

			convertView.setTag(childViewHolder);
		}else{
			childViewHolder = (ChildViewHolder) convertView.getTag();
		}

		GroupInfo info = groupList.get(groupPosition);
		List<UserInfo> userInfoList = info.getMemberList();

		if(userInfoList == null
				|| userInfoList.get(childPosition) == null){
			return convertView;
		}
		final UserInfo userInfo = userInfoList.get(childPosition);
		Log.d(TAG, "getChildView userInfo = " + userInfo + " mcIdInfo = " + mcIdInfo);
		childViewHolder.userInfoLy.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				goToMsgActivity(userInfo);
			}
		});

		if(childPosition == userInfoList.size()-1){
			childViewHolder.divider.setVisibility(View.GONE);
		}else{
			childViewHolder.divider.setVisibility(View.VISIBLE);
		}

		if(userInfo == null){
			return convertView;
		}
		String name = userInfo.getName();

		if(userInfo.isSys()
				|| userInfo.isDac()){//DAC
			childViewHolder.videoBtn.setVisibility( View.VISIBLE );
			if(userInfo.getOnlineState() == 1)
			{
				childViewHolder.userIcon.setImageResource(Drawable_online[DAC_TYPE]);
				setDrawableEnable(childViewHolder.userIcon, true);
				setDrawableEnable(childViewHolder.userIcon, true);
				setDrawableEnable(childViewHolder.videoBtn, true);
			}
			else
			{
				childViewHolder.userIcon.setImageResource(Drawable_offline[DAC_TYPE]);
				setDrawableEnable(childViewHolder.userIcon, false);
				setDrawableEnable(childViewHolder.userIcon, false);
				setDrawableEnable(childViewHolder.videoBtn, false);
			}
			childViewHolder.voiceBtn.setVisibility(View.VISIBLE);

		}else if(userInfo.isCamera()){//Camera
			childViewHolder.userIcon.setImageResource(Drawable_online[CAMERA_TYPE]);
			childViewHolder.videoBtn.setVisibility( View.VISIBLE );
			childViewHolder.voiceBtn.setVisibility(View.GONE);

		}else{//normal user
			if(userInfo.getOnlineState() == 1){
				childViewHolder.userIcon.setImageResource(Drawable_online[USER_TYPE]);
				setDrawableEnable(childViewHolder.userIcon, true);
				setDrawableEnable(childViewHolder.videoBtn, true);
				setDrawableEnable(childViewHolder.voiceBtn, true);
			}else{
				childViewHolder.userIcon.setImageResource(Drawable_offline[USER_TYPE]);
				setDrawableEnable(childViewHolder.userIcon, false);
				setDrawableEnable(childViewHolder.videoBtn, false);
				setDrawableEnable(childViewHolder.voiceBtn, false);
			}
			childViewHolder.voiceBtn.setVisibility( View.VISIBLE );
			childViewHolder.videoBtn.setVisibility( View.VISIBLE );
		}

//		if(userInfo.isSameOne(mcIdInfo)){
//			//childViewHolder.mySelf.setVisibility(View.VISIBLE);
//			String str = ctx.getResources().getString(R.string.name_myself);
//			//childViewHolder.mySelf.setVisibility(View.GONE);
//			childViewHolder.name.setText(name+"."+str);
//		}else{
//			childViewHolder.name.setText(name);
//			childViewHolder.mySelf.setVisibility(View.GONE);
//		}
		childViewHolder.name.setText(name);
		if(userInfo.isSameOne(mcIdInfo)){
			Log.d(TAG, "getChildView in");
			childViewHolder.mySelf.setVisibility(View.VISIBLE);
//			String str = ctx.getResources().getString(R.string.name_self);
//			childViewHolder.mySelf.setText(str);
		}else{
			childViewHolder.mySelf.setVisibility(View.GONE);
		}
//		childViewHolder.mySelf.setVisibility(View.GONE);

		childViewHolder.userIcon.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showUserInfo(userInfo);
			}
		});
		childViewHolder.videoBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				doClickCallBtnAction(userInfo, true ,CallAttr.CallType_Private);
			}
		});

		childViewHolder.voiceBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				doClickCallBtnAction(userInfo, false,CallAttr.CallType_Private);
			}
		});
		return convertView;
	}

	private void goToMsgActivity(AddrInfo addrInfo)
	{
		Log.d(TAG, "goToMsgActivity addrInfo = " + addrInfo);
		//		AddrInfo addrInfo = groupInfo.getUserList().get(childPosition);
		Intent intent = new Intent(ctx, SessionActivity.class);
		intent.putExtra(SessionActivity.KEY, addrInfo);
		ctx.startActivity(intent);	
	}

	private void setDrawableEnable(ImageView userIcon, boolean enable) {
		// TODO Auto-generated method stub
		int alpha = (int)(enable ? 255*1 : 255*0.5);
		userIcon.setImageAlpha(alpha);
	}

	private void setOffLineIcon(ImageView userIcon){
		userIcon.setBackgroundColor(Color.parseColor("#555555"));
	}


	private void showUserInfo(UserInfo info){
		if(DEBUG){
			CommDlg.show(ctx, info.toString());
		}
	}

	public GroupInfo getGroup(int groupPosition) {   
		if(groupList == null
				|| groupList.size() <= 0)
		{
			return null;
		}
		GroupInfo info = groupList.get(groupPosition);
		if(info != null)
		{
			return info;
		}
		return null;
	}   

	public boolean hasStableIds() {   
		return true;   
	} 

	public boolean isChildSelectable(int groupPosition, int childPosition) {   
		return true;   
	}   

		private void doClickCallBtnAction(UserInfo userInfo, boolean useVideo, int videoType){
		String num = userInfo.getAddrId();
		Log.d(TAG, "doClickCallBtnAction userInfo = " + userInfo + " useVideo = " + useVideo);
		if(userInfo.isSameOne(mcIdInfo)){
			CommDlg.show(ctx, ctx.getResources().getString(R.string.call_self));
			return;
		}

		//		if( useVideo && !isCameraUseed()){
		//			return;
		//		}
		int callType = CallAttr.CallType_Private;
		if(userInfo.isCamera())
		{
			String name = userInfo.getName();
			String rtspStr = userInfo.getAddrId();
//			CameraInfo info = userInfo.toCameraInfo();
			Log.d(TAG, "doClickCallBtnAction name = " + name + " rtspStr = " + rtspStr);
//			Intent intent = new Intent();
//			intent.setClass(ctx, CameraActivity.class);
//			intent.putExtra("name", name);
//			intent.putExtra("rtsp", rtspStr);
//			//		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			ctx.startActivity(intent);
		}
		else
		{
			if(useVideo)
			{
				callType = videoType;
			}
			SysApplication app = ((SysApplication) ctx.getApplicationContext());
			Product product = app.getProduct();
			product.getPCallEventAdapter().makePrivateCall(userInfo, callType,useVideo,false,"");
		}
		//		CallFunction.makePrivateCall(ctx,userInfo, callType,useVideo);
	}

	//	private boolean isCameraUseed()
	//	{
	//		Camera camera = null;
	//		try{
	//			if( null == (camera=Camera.open() ) ){
	//				CommDlg.showToast( ctx, ctx.getString( R.string.confirm_dev_camera ) );
	//				Log.e(TAG, "OpenCamera error");
	//				return false;
	//			}
	//		}catch(Exception ex){
	//			CommDlg.showToast( ctx, ctx.getString( R.string.confirm_dev_camera ) );
	//			Log.e(TAG, "OpenCamera error:" + ex.getMessage( ) );
	//			return false;
	//		}
	//		if(null!= camera){
	//			camera.release( );
	//		}
	//		return true;
	//	}

	public final class ChildViewHolder {
		public View userInfoLy;
		public TextView  name;
		public ImageView videoBtn;
		public ImageView userIcon;
		public ImageView voiceBtn;
		public TextView mySelf;
		public View divider;
	}

	public final class GroupViewHolder {

		public View userInfoLy;
		public TextView  name;
		public ImageView userIcon;
		public TextView defaultTv;
//		public TextView curTv;
		public ImageView indicator;
		public View divider;
	}

	public void setMyLoginInfo(AddrInfo myInfo){
		this.mcIdInfo = myInfo;
	}
	public void setDefaultGroupInfo(GroupInfo defaultGroupInfo) {
		this.curGroupInfo = defaultGroupInfo;
	}
}   


